package com.yan.core.annotations.parse;


import com.yan.core.annotations.Autowired;
import com.yan.core.annotations.Repository;
import com.yan.core.annotations.Service;
import com.yan.core.annotations.Transactional;
import com.yan.utils.StringUtils;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class ParseAnnotation {

    /**
     * 过滤掉不是 Service 注解的
     */
    public static List<Class<?>> filterNoService(List<Class<?>> pageClass) {
        List<Class<?>> result = new ArrayList<>();
        for (Class<?> aClass : pageClass) {
            for (Annotation annotation : aClass.getAnnotations()) {
                Service service = getService(annotation);
                if (service != null) {
                    result.add(aClass);
                }
            }
        }
        return result;
    }

    /**
     * 过滤掉不是 Autowired 注解的
     */
    public static List<Field> filterNoAutowired(Field[] allField) {
        List<Field> result = new ArrayList<>();
        for (Field field : allField) {
            for (Annotation annotation : field.getAnnotations()) {
                Autowired autowired = getAutowired(annotation);
                if (autowired != null) {
                    result.add(field);
                }
            }
        }
        return result;
    }

    /**
     * 判断是不是 Transactional 注解的
     */
    public static Boolean isTransactional(Class<?> beanClass) {
        for (Annotation annotation : beanClass.getAnnotations()) {
            Transactional transactional = getTransactional(annotation);
            if (transactional != null) {
                return true;
            }
        }
        return false;
    }

    /**
     * 解析 beanName
     */
    public static String parseBeanName(Class<?> componentClass) {
        Annotation[] annotations = componentClass.getAnnotations();
        for (Annotation annotation : annotations) {
            String value = parseValue(annotation);
            if (StringUtils.isNotBlank(value)) {
                return value;
            }
        }
        return StringUtils.lowerFirstChar(componentClass.getSimpleName());
    }

    /**
     * 解析 FiledName
     */
    public static String parseFiledName(Field field) {
        for (Annotation annotation : field.getAnnotations()) {
            String value = parseValue(annotation);
            if (StringUtils.isNotBlank(value)) {
                return value;
            }
        }
        return field.getName();
    }

    private static String parseValue(Annotation annotation) {
        Service service = getService(annotation);
        if (service != null) {
            String value = service.value();
            return value;
        }
        Autowired autowired = getAutowired(annotation);
        if (autowired != null) {
            Repository repository = getRepository(annotation);
            if (repository != null) {
                String value = repository.value();
                return value;
            }
        }
        return null;
    }

    private static Service getService(Annotation annotation) {
        if (annotation instanceof Service) {
            return (Service) annotation;
        }
        return null;
    }

    private static Autowired getAutowired(Annotation annotation) {
        if (annotation instanceof Autowired) {
            return (Autowired) annotation;
        }
        return null;
    }

    private static Repository getRepository(Annotation annotation) {
        if (annotation instanceof Repository) {
            return (Repository) annotation;
        }
        return null;
    }

    private static Transactional getTransactional(Annotation annotation) {
        if (annotation instanceof Transactional) {
            return (Transactional) annotation;
        }
        return null;
    }

}
